package com.rc.web.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.google.common.collect.ImmutableMap;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.rc.common.constant.CommentType;
import com.rc.common.util.RabbitMqHelper;
import com.rc.pojo.dto.comment.CommentAddDTO;
import com.rc.pojo.dto.comment.CommentPageQuery;
import com.rc.pojo.po.Comment;
import com.rc.pojo.po.Consumer;
import com.rc.pojo.vo.comment.CommentPageVO;
import com.rc.pojo.vo.comment.CommentVO;
import com.rc.web.converter.CommentConverter;
import com.rc.web.mapper.CommentMapper;
import com.rc.web.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 评论服务实现类
 * 提供评论相关的业务逻辑实现
 */
@Service
@RequiredArgsConstructor
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    // 评论转换器
    private final CommentConverter commentConverter;
    // 用户服务
    private final ConsumerService consumerService;
    // 领养服务
    private final AdoptionService adoptionService;
    // 活动服务
    private final ActivityService activityService;
    // RabbitMQ工具类
    private final RabbitMqHelper rabbitMqHelper;
    // 农场服务
    private final FarmService farmService;

    /**
     * 根据类型获取评论列表
     * @param itemId 评论项ID
     * @param type 评论类型
     * @return 评论VO列表
     */
    @Override
    public List<CommentVO> listByType(Long itemId, Integer type) {
        List<Comment> comments = mapper.selectListByType(itemId,type);
        return comments.stream()
                .map(commentConverter::toVO)
                .toList();
    }

    /**
     * 增加评论浏览量
     * @param id 评论ID
     */
    @Override
    public void viewsAdd(Long id) {
        mapper.viewsAdd(id);
    }

    /**
     * 获取评分
     * @param itemId 评论项ID
     * @param type 评论类型
     * @return 评分
     */
    @Override
    public Double getScore(Long itemId, Integer type) {
        return mapper.selectScore(itemId,type);
    }

    /**
     * 获取推荐数
     * @param itemId 评论项ID
     * @param type 评论类型
     * @return 推荐数
     */
    @Override
    public Integer getRecommended(Long itemId, Integer type) {
        return mapper.selectRecommended(itemId,type);
    }

    /**
     * 获取每个评论项的最后三条好评
     * @param itemIds 评论项ID列表
     * @param type 评论类型
     * @return 评论项ID与评论列表的映射
     */
    @Override
    public Map<Long,List<Comment>> getItemLastThreeFavorable(List<Long> itemIds, Integer type) {
        List<Comment> comments = mapper.selectByItemIds(itemIds,type,4);
        return comments.stream()
                .collect(Collectors.groupingBy(Comment::getItemId));
    }

    /**
     * 添加评论
     * @param dto 评论添加DTO
     */
    @Override
    public void add(CommentAddDTO dto) {
        Long itemId = dto.getItemId();
        Integer type = dto.getType();

        long loginIdAsLong = StpUtil.getLoginIdAsLong();

        Comment comment = commentConverter.addDTO2PO(dto);

        //1、获取当前用户头像 昵称等信息
        Consumer consumer = consumerService.getById(loginIdAsLong);
        comment.setAvatar(consumer.getAvatar());
        comment.setNickName(consumer.getNickName());

        // 根据类型设置农场ID
        Long farmId = switch (type){
            case CommentType.FARM -> farmService.getById(itemId).getId();
            case CommentType.ADOPTION -> adoptionService.getById(itemId).getFarmId();
            case CommentType.ACTIVITY -> activityService.getById(itemId).getFarmId();
            default -> null;
        };
        comment.setFarmId(farmId);

        this.save(comment);

        // 发送消息更新评分
        ImmutableMap<String,Object> map = ImmutableMap.<String,Object>builder()
                .put("itemId",comment.getItemId())
                .put("type",comment.getType())
                .build();
        rabbitMqHelper.sendMessage("comment.direct","update", map);
    }

    /**
     * 根据用户ID更新评论
     * @param comment 评论对象
     */
    @Override
    public void updateByConId(Comment comment) {
        mapper.updateByConId(comment);
    }

    /**
     * 分页查询评论
     * @param dto 分页查询条件
     * @return 分页结果
     */
    @Override
    public Page<CommentPageVO> getPage(CommentPageQuery dto) {
        return mapper.selectPage(dto);
    }

    /**
     * 根据ID获取评论
     * @param id 评论ID
     * @return 评论VO
     */
    @Override
    public CommentVO getOneById(Long id) {
        Comment comment = mapper.selectOneById(id);
        return commentConverter.toVO(comment);
    }

    /**
     * 根据ID删除评论
     * @param id 评论ID
     */
    @Override
    public void deletedOneById(Long id) {
        Comment byId = this.getById(id);
        this.removeById(id);

        // 发送消息更新评分
        ImmutableMap<String,Object> map = ImmutableMap.<String,Object>builder()
                .put("itemId",byId.getItemId())
                .put("type",byId.getType())
                .build();

        rabbitMqHelper.sendMessage("comment.direct","update", map);
    }
}
